/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.ioHandling.fileWriter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import systole.exceptions.ExceptionIO;
import systole.ioHandling.logs.SystoleLogger;

/**
 *
 * @author jmj
 */
public class FileWriterSystlole {

    private Writer writer = null;

    /**
     * @param fileName Name of file to create and write.
     * @throws ExceptionIO 
     */
    public FileWriterSystlole(String fileName) throws ExceptionIO {
        try {
            this.writer = new BufferedWriter(new FileWriter(fileName));
        } catch (IOException ex) {
            SystoleLogger.getInstance().logError(ex.getMessage());
            throw new ExceptionIO(ex.getMessage());
        }
    }

    
    /**
     * @param line Line to write on the file.
     * @throws ExceptionIO 
     */
    public void write(String line) throws ExceptionIO {

        if (this.writer != null) {

            try {
                //FileWriter always assumes default encoding is OK!
                this.writer.write(line);

            } catch (IOException ex) {
               SystoleLogger.getInstance().logError(ex.getMessage());  
               throw new ExceptionIO(ex.getMessage());
            }
        }
   }


    /**
     * Close the file.
     */
    public void close(){
    if (this.writer != null) {
          try {
                this.writer.close();

            } catch (IOException ex) {
               SystoleLogger.getInstance().logError(ex.getMessage());
            }
        }
    }


}
